<style>
  .layui-badge-rim {
    height: 20px;
    border-width: 1px;
    border-style: solid;
    background-color: #eef4f5;
    font: 14px Helvetica Neue, Helvetica, PingFang SC, Tahoma, Arial, sans-serif;
    color: #6e667d;
  }
</style>

<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <div class="layui-form lay-header">
        <div class="toolbar">
          <div class="layui-form-item lay-query-content">
            <div class="lay-query-box">
              <div class="query-lt">
                <!-- <div class="layui-inline">
                  <label class="layui-form-label w-auto">来源：</label>
                  <div class="layui-input-inline">
                    <select name="status" lay-filter="status" class="common-select-search">
                      <option value="">请选择</option>
                      <option value="0">客户订单</option>
                      <option value="1">自研产品</option>
                    </select>
                  </div>
                </div> -->
                <!-- 是否默认 -->
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">基准书编号:</label>
                  <div class="layui-input-inline">
                    <input
                      type="text"
                      name="code"
                      lay-filter="code"
                      class="layui-input"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">基准书类型:</label>
                  <div class="layui-input-inline">
                    <select name="type" lay-filter="type">
                      <option value="">请选择</option>
                      <option value="1">打样基准书</option>
                      <option value="2">正式基准书</option>
                    </select>
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">内部sku料号:</label>
                  <div class="layui-input-inline">
                    <input
                      type="text"
                      name="skuCode"
                      lay-filter="skuCode"
                      class="layui-input"
                    />
                  </div>
                </div>
                <div class="layui-inline">
                  <label class="layui-form-label w-auto">内部sku名称:</label>
                  <div class="layui-input-inline">
                    <input
                      type="text"
                      name="skuName"
                      lay-filter="skuName"
                      class="layui-input"
                    />
                  </div>
                </div>
              </div>
              <div class="query-rt">
                <div class="layui-inline">
                  <button
                    class="layui-btn icon-btn color-green"
                    lay-filter="benchmarkFormSubSearchLog"
                    lay-submit
                    layui-form-keyDownSearch="true"
                  >
                    查询
                  </button>
                  <button
                    class="layui-btn icon-btn color-white"
                    lay-filter="resets"
                    lay-submit
                  >
                    重置
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="lay-btn-rows">
          <div class="lay-btn-box">
            <div class="lay-btn-lt">
              <button
                id="benchmarkBookAdd"
                type="button"
                class="layui-btn icon-btn color-reseda"
              >
                添加基准书
              </button>
            </div>
            <div class="lay-btn-rt"></div>
          </div>
        </div>
      </div>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="benchmarkBookInfo"
        lay-filter="benchmarkBookInfo"
      ></table>
    </div>
  </div>
</div>

<script type="text/html" id="viewAttachmentHtml">
  <div id="example1" style="height: 100%;"></div>
</script>
<!-- 状态(1. 待初审 2.待排产 3.打样中 4.待校对 5.待终审 6.已定型) -->
<!-- 表格操作列 -->
<script type="text/html" id="benchmarkTableBar">
  <a class="layui-btn  layui-btn-xs" lay-event="edit">修改</a>
  <!-- {{# if(d.status == 1 ||d.status == 0) { }} -->

  <!-- {{# } }} -->
  <!-- {{# if(d.status == 1 ) { }} -->
  <a class="layui-btn  layui-btn-xs" lay-event="audit">审核</a>
  <!-- {{# } }} -->
  <a class="layui-btn  layui-btn-xs" lay-event="copy">复制基准书</a>
  <!-- {{# if(d.status != 7 ) { }} -->

  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">作废</a>
  <!-- {{# } }} -->
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail"
    >详情</a
  >
  <!-- <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="Production">生产</a> -->
</script>

<!-- js部分 -->
<script>
  layui.use(
    [
      "layer",
      "form",
      "table",
      "laydate",
      "admin",
      "laytpl",
      "tableTreeDj",
      "config",
    ],
    function () {
      var $ = layui.jquery;
      var layer = layui.layer;
      var table = layui.table;
      var form = layui.form;
      var config = layui.config;
      var admin = layui.admin;

      //产品属性
      admin.getDictList(
        "assemblyNature",
        "product_assembly_nature",
        null,
        "产品属性"
      );

      // admin.initDataSelectTree(
      //   true,
      //   "enterprise/product/admin/productType/getTreeData",
      //   { materialNatureList: [2, 3, 7] },
      //   "productTypeName",
      //   null,
      //   "物料分类",
      //   "productTypeId",
      //   function (row) {
      //     $("input[name='productTypeId']").val(row.arr[0].value);
      //   }
      // );

      //渲染下拉
      form.render("select");

      // 渲染表格
      var insTb = table.render({
        id: "#benchmarkBookInfo",
        elem: "#benchmarkBookInfo",
        url: config.base + "enterprise/benchmarkBook/admin/benchmarkBook/list",
        page: true,
        cellMinWidth: 100,
        cols: [
          [
            { type: "numbers", title: "序号", align: "center" },
            { field: "code", title: "基准书编号", align: "center", width: 160 },
            { field: "versionInterval", title: "基准书版本号", align: "center", width: 160, templet: function (d) {
              return d.skuCode + "-"+d.versionInterval;
            } },
            { field: "typeName", title: "基准书类型", align: "center" },
            { field: "skuCode", title: "内部sku料号", align: "center" },
            { field: "skuName", title: "内部sku名称", align: "center" },
            { field: "skuSpec", title: "内部规格码", align: "center" },
            // { field: "sourceName", title: "来源", align: "center" },
            // {
            //   field: "productTypeName",
            //   align: "center",
            //   title: "内部产品类别",
            // },
            { field: "materialNatureName", align: "center", title: "产品性质" },
            { field: "productAttrName", align: "center", title: "产品属性" },
            { field: "statusName", align: "center", title: "状态" },
            { field: "createTime", align: "center", title: "创建时间" },
            {
              align: "center",
              toolbar: "#benchmarkTableBar",
              title: "操作",
              fixed: "right",
              width: 320,
            },
          ],
        ],
        done: function (res, curr, count) {
          //移除按钮
          var menuButton = config.getMenuButton(location.hash);
          console.log("menuButtton", menuButton);
          // if (menuButton.indexOf("搜索") == -1) {
          //     $('.layui-form.toolbar').remove();
          // }
          // if (menuButton.indexOf("修改") == -1) {
          //     $("[lay-event='edit']").remove();
          // }
          // if (menuButton.indexOf("删除") == -1) {
          //     $("[lay-event='del']").remove();
          // }
        },
      });

      // 工具条点击事件
      table.on("tool(benchmarkBookInfo)", async function (obj) {
        var data = obj.data;
        if (obj.event === "detail") {
          //详情
          // showDetailModel(data);
          admin.formOpen({
            area: ["94%", "90%"],
            offset: "25px",
            title: "生成基准书",
            path: "components/res/benchmarkBook/benchmarkBookForm.html",
            success: function () {
              parentData.eventName = "detail";
              parentData.data = data;
            },
          });
        } else if (obj.event === "edit") {
          //详情
          // showDetailModel(data);
          admin.formOpen({
            area: ["94%", "90%"],
            offset: "25px",
            title: "生成基准书",
            path: "components/res/benchmarkBook/benchmarkBookForm.html",
            success: function () {
              console.log(data);

              parentData.eventName = "edit";
              parentData.data = data;
            },
          });
        } else if (obj.event === "Production") {
          console.log("Production", data);
          admin.formOpen({
            type: 1,
            area: ["94%", "90%"],
            offset: "25px",
            title: "添加生产计划",
            path: "components/productionPlan/productionPlanForm.html",
            success: function () {
              parentData.eventName = "add";
              parentData.productDetailId = data.productDetailId;
              parentData.type = "基准书生成";
            },
          });
        } else if (obj.event === "copy") {
          //复制
          layer.confirm(
            "确定复制该基准书生成一份新的基准书吗？",
            { icon: 3, title: "提示" },
            async function (index) {
              const res = await admin.reqPro(
                "enterprise/benchmarkBook/admin/benchmarkBook/detail/" +
                  data.id,
                {}
              );
              const dataObj = res.data;
              delete dataObj.id;
              console.log("dataObj", dataObj);

              admin.req(
                "enterprise/benchmarkBook/admin/benchmarkBook/generateBenchmarkBook",
                JSON.stringify(dataObj),
                function (res) {
                  layer.msg("复制成功！", { icon: 1 });
                  layer.closeAll("page");
                  table.reload("#benchmarkBookInfo");
                  layer.close(index);
                },
                "POST",
                true,
                "application/json; charset=utf-8"
              );
            }
          );
        } else if (obj.event === "audit") {
          layer.confirm(
            "初审是否通过？",
            { icon: 3, title: "提示" },
            function (index) {
              admin.req(
                "enterprise/benchmarkBook/admin/benchmarkBook/benchmarkBookChange/" +
                  data.id +
                  "/" +
                  2,
                {},
                function (res) {
                  layer.msg("审核成功！", { icon: 1 });
                  layer.closeAll("page");
                  table.reload("#benchmarkBookInfo");
                },
                "POST"
              );
            }
          );
        } else if (obj.event === "cancel") {
          layer.confirm(
            "确定作废该基准书吗？",
            { icon: 3, title: "提示" },
            function (index) {
              admin.req(
                "enterprise/benchmarkBook/admin/benchmarkBook/voidBook/" +
                  data.id,
                {},
                function (res) {
                  layer.msg("作废成功！", { icon: 1 });
                  layer.closeAll("page");
                  table.reload("#benchmarkBookInfo");
                  layer.close(index);
                },
                "POST",
                true,
                "application/json; charset=utf-8"
              );
            }
          );
        }
      });
      $("#benchmarkBookAdd").on("click", function () {
        admin.formOpen({
          area: ["94%", "90%"],
          offset: "25px",
          title: "生成基准书",
          path: "components/res/benchmarkBook/benchmarkBookForm.html",
          success: function () {
            parentData.eventName = "add";
            parentData.data = {};
            parentData.type = "基准书列表";
          },
        });
      });
      function showViewAttachmentModel(data) {
        admin.open({
          type: 1,
          area: ["60%", "80%"],
          offset: "65px",
          title: "查看附件",
          content: $("#viewAttachmentHtml").html(),
          success: function () {
            if (data.attachment && data.attachment.indexOf(".pdf") !== -1) {
              PDFObject.embed(data.attachment, "#example1");
            } else {
              var attachmentHtml = "";
              data.attachment.split(",").forEach((attachmentUrl, index) => {
                attachmentHtml +=
                  '<div><div><img src="' + attachmentUrl + '"></div></div>';
              });
              $("#example1").html(attachmentHtml);
            }
          },
        });
      }

      // 搜索
      form.on("submit(benchmarkFormSubSearchLog)", function (data) {
        insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
      });

      // 重置搜索
      form.on("submit(resets)", (data) => {
        insTb.reload(
          { where: admin.resetSearch(data.field), page: { curr: 1 } },
        );
      });

      //显示详情
      function showDetailModel(data) {
        admin.putTempData("benchmarkData", data);
        let title = "详情";
        let path = "components/res/benchmarkBook/benchmarkDetailFrom.html";
        admin.formOpen({
          type: 1,
          area: ["1580px", "85%"],
          style: "font-size: 20px;",
          title: title,
          path: path,
          finish: function () {
            table.reload("benchmarkBookInfo", {});
          },
        });
      }
    }
  );
</script>
